package com.simple.common.entity.po;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.core.type.TypeReference;
import com.simple.common.entity.po.sys.Channel;
import com.simple.common.framework.context.Contexts;
import lombok.Data;
import lombok.SneakyThrows;

import java.util.List;
import java.util.Map;

/**
 * 渠道回调我的
 * @author yasud
 * @date 2020年10月12日 08:57
 * @description
 */
@Data
@TableName("t_notify_channel")
public class NotifyChannel extends BaseTimeEntity {

    @TableId(type = IdType.ASSIGN_ID)
    private Long id;

    /**
     * 回调的参数参数有可能从. body,或者路径获得
     */
    private String body;

    /**
     * get请求下url里面的参数或者form 表单参数
     */
    private String param;

    /**
     * 请求头
     */
    private String header;

    /**
     * 是否已经被处理
     */
    private Boolean isProcessed;

    /**
     * 回调的类型
     */
    private Type type;

    /**
     * 是否退款
     */
    private Boolean isRefund;

    /**
     * 是否需要发送通知给商户
     */
    private Boolean isSendMerchant;

    /**
     * 如果是退款退款流水
     */
    private Long refundTradeInventoryId;

    /**
     * 对于交易id
     */
    private Long tradeId;

    /**
     * 关联渠道
     */
    private Long channelId;

    /**
     * 关联appId
     */
    private String appId;

    /**
     * 关联渠道
     */
    @TableField(exist = false)
    private Channel channel;

    /**
     *
     */
    public enum Type {
        /**
         * 渠道配置
         */
        FIX,

        /**
         * 字段传递
         */
        DYNAMIC
    }

    @SneakyThrows
    public Map<String, Object> readMapFromBody() {
        Map<String, Object> map = Contexts.OBJECTMAPPER.readValue(body, new TypeReference<Map<String, Object>>() {
        });
        return map;
    }

    @SneakyThrows
    public List<Map<String, Object>> readListFromBody() {
        List<Map<String, Object>> maps = Contexts.OBJECTMAPPER.readValue(body, new TypeReference<List<Map<String, Object>>>() {
        });
        return maps;
    }

    @SneakyThrows
    public Map<String, Object> readMapFromHeader() {
        Map<String, Object> map = Contexts.OBJECTMAPPER.readValue(header, new TypeReference<Map<String, Object>>() {
        });
        return map;
    }

    @SneakyThrows
    public Map<String, Object> readMapFromParam() {
        Map<String, Object> map = Contexts.OBJECTMAPPER.readValue(param, new TypeReference<Map<String, Object>>() {
        });
        return map;
    }

}
